package cn.tedu.charging.order.config;

import cn.tedu.charging.common.constant.RabbitConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class DelayRabbitConfiguration {
    //声明组件 要使用组件名称 包括交换机 队列 绑定路由
    //声明业务交换机
    @Bean
    public DirectExchange chargingExchange(){
        return new DirectExchange(RabbitConstants.CHARGING_EX);
    }
    //声明业务队列 非十句话,绑定延迟死信交换机,提供死信交换机路由key,设置ttl 1分钟毫秒数
    @Bean
    public Queue chargingQueue(){
        return QueueBuilder.nonDurable(RabbitConstants.CHARGING_TTL_QUEUE)
                .deadLetterExchange(RabbitConstants.DELAY_CHARGING_EX)
                .deadLetterRoutingKey(RabbitConstants.DELAY_CHARGING_ROUTING_KEY)
                .ttl(1000*60).build();
    }
    //将充电业务队列和业务交换机绑定
    @Bean
    public Binding bindCharging(){
        return BindingBuilder.bind(chargingQueue()).to(chargingExchange()).with(
                RabbitConstants.CHARGING_ROUTING_KEY);
    }
    @Bean
    public DirectExchange delayChargingExchange(){
        return new DirectExchange(RabbitConstants.DELAY_CHARGING_EX);
    }

    @Bean
    public Queue delayChargingQueue(){
        return QueueBuilder.nonDurable(RabbitConstants.DELAY_CHARGING_QUEUE).build();
    }
    //绑定死信队列和死信交换机
    @Bean
    public Binding bindDelay(){
        return BindingBuilder.bind(delayChargingQueue()).to(delayChargingExchange())
                .with(RabbitConstants.DELAY_CHARGING_ROUTING_KEY);
    }
}
